/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;
using FileSystemDatabase;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of ManufacturerStore.
	/// </summary>
	public class ManufacturerStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		private JsonDatabase db;
		public void PersistManufacturers(List<Dictionary<string,object>> Manufacturers)
		{
			db.Write<List<Dictionary<string,object>>>(Manufacturers,_site.Name);
		}

		public void PersistManufacturer(Dictionary<string,object> Manufacturer)
		{
			db.Write<Dictionary<string,object>>(Manufacturer,Manufacturer["manufacturers_id"].ToString());
		}
		
		public ManufacturerStore(Site site,HtmlWeb client)
		{
			db = new JsonDatabase("sites/" + site.Name + "/Manufacturer");
			_site = site;
			_client = client;
		}
		
		private Dictionary<string,object> GetManufacturer(string manufacturerId)
		{
			List<Dictionary<string,object>> entries = new List<Dictionary<string,object>>();
			string url = _site.Url + "/admin/json.php?action=load_manufacturer&manufacturers_id=" + manufacturerId + "&module=manufacturers";
			
			HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "data")
				{					
					Dictionary<string,object> manufacturer = (Dictionary<string,object>)json[key];					
					if(manufacturer["manufacturers_image"] != null)
					{
						string image = manufacturer["manufacturers_image"].ToString();
						Utility.DownloadImage(_site.Url + "/images/manufacturers/" + image,"sites/" + _site.Name + "/Manufacturer/images/");
					}
					entries.Add(manufacturer);
				}
			}			
			
			return entries[0];
		}
		
		public List<Dictionary<string,object>> GetManufacturers()
		{
			List<Dictionary<string,object>> records = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_manufacturers&module=manufacturers");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> record = (Dictionary<string,object>)recs[i];	
						Dictionary<string,object> manufacturer = GetManufacturer(record["manufacturers_id"].ToString());
						records.Add(manufacturer);												
					}									
				}
			}			
			
			return records;
		}		
	}
}
